#include "Router.h"
#include <iostream>
#include <string>

RouterEndpoint::RouterEndpoint(const std::string& address) {
    context = zmq::context_t();
    router = zmq::socket_t(context, zmq::socket_type::router);
    router.bind(address);
}

void RouterEndpoint::run() {
    while (true) {
        zmq::message_t identity;
        zmq::message_t message;

        // 接收身份标识和消息内容
        router.recv(identity, zmq::recv_flags::none);
        router.recv(message, zmq::recv_flags::none);

        // 打印身份标识为十六进制
        std::cout << "Received message from: ";
        for (size_t i = 0; i < identity.size(); ++i) {
            printf("%02x", static_cast<unsigned char*>(identity.data())[i]);
        }
        std::cout << std::endl;

        std::cout << "Received: " << message.to_string() << std::endl;

        zmq::message_t reply_message("Response from server", 20); // 创建回复消息
        router.send(identity, zmq::send_flags::sndmore); // 发送身份标识
        router.send(reply_message, zmq::send_flags::none); // 发送消息内容
    }
}
